package ua.org.nuos.sdms.middle.testutils;

import org.dbunit.DatabaseUnitException;
import org.dbunit.database.*;
import org.dbunit.dataset.FilteredDataSet;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ReplacementDataSet;
import org.dbunit.dataset.filter.ITableFilter;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.ext.postgresql.PostgresqlDataTypeFactory;
import org.dbunit.operation.DatabaseOperation;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 11.10.11
 * Time: 19:27
 * To change this template use File | Settings | File Templates.
 */
public class DBUtil {
    private IDatabaseConnection connection;
    private String dataSetsRoot;
    private boolean connectionClosed;
    private Properties connectionProperties;

    public DBUtil() {
        try {
            connectionProperties = getConfiguration();
            dataSetsRoot = connectionProperties.getProperty("dataset_location");
            setupConnection(connectionProperties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private Properties getConfiguration() throws IOException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src/test/resources/db.properties"));
        return properties;
    }

    private void setupConnection(Properties connectionProperties) throws SQLException, DatabaseUnitException {
        Connection jdbcConnection = DriverManager.getConnection(
                connectionProperties.getProperty("jdbc_url"),
                connectionProperties.getProperty("user"),
                connectionProperties.getProperty("password"));
        connection = new DatabaseConnection(jdbcConnection);
        connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new PostgresqlDataTypeFactory());
        connectionClosed = false;
    }

    public void setUpDatabase(String[] dataSets) {
        if ((dataSets == null) || (dataSets.length == 0)) {
            return;
        }
        try {
            if (connectionClosed) {
                setupConnection(connectionProperties);
            }
            IDataSet emptySet = new FlatXmlDataSet(new File(dataSetsRoot + "empty.xml"));
            DatabaseOperation.CLEAN_INSERT.execute(connection, emptySet);

//            IDataSet sequenceSet = new FlatXmlDataSet(new File(dataSetsRoot + "sequence.xml"));
//            DatabaseOperation.CLEAN_INSERT.execute(connection, sequenceSet);

            for (String dataSetName : dataSets) {
                IDataSet dataSet = new YamlDataSet(new File(dataSetsRoot + dataSetName));
                DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
            }
        } catch (Exception ex) {
            closeConnection();
            throw new RuntimeException(ex);
        }
    }

    public void closeConnection() {
        try {
            connection.close();
            connectionClosed = true;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

